/*
 * Copyright (c) 2006, 2022, webrx.cn All rights reserved.
 *
 */
package cn.webrx.controller;

import cn.webrx.mapper.BookMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.core.SpringVersion;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>Project: mybatis2022 - IndexController
 * <p>Powered by webrx On 2022-04-15 11:06:25
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
@RestController @Slf4j
public class IndexController {
    @Autowired
    BookMapper bm;

    @RequestMapping  @Cacheable(value = "default")
    public Map<String, Object> index() {
        log.info("hello world");
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "查询成功");
        map.put("sversion", SpringVersion.getVersion());
        System.out.println("首页查询");
        map.put("data", bm.queryAll());
        return map;
    }

    @RequestMapping("/clear")
    @CacheEvict(value = "default", allEntries = true)
    public Map<String, Object> clear() {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "缓存数据清除所有完成");
        return map;
    }


    @RequestMapping("/cid")
    @CacheEvict(value = "default", key = "#id")
    public Map<String, Object> cid(int id) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "清除id=" + id + "的缓存");
        return map;
    }

    @RequestMapping("/show")
    @Cacheable(value = "default", key = "#id")
    public Map<String, Object> show(int id) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "查询成功");
        map.put("data", bm.queryById(id));
        System.out.println("设置id=" + id + "的缓存");
        return map;
    }


}
